﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using OpenIddict.Abstractions;
using System.Threading.Tasks;

namespace openiddict.demo.Controllers
{
    [Route("api/application")]
    [ApiController]
    public class ApplicationController : ControllerBase
    {
        private readonly IOpenIddictApplicationManager applicationManager;
        public ApplicationController(IOpenIddictApplicationManager applicationManager)
        {
            this.applicationManager = applicationManager;
        }


        [HttpPost("add")]
        public async Task<IActionResult> Add(string name)
        {
            var info = new OpenIddictApplicationDescriptor
            {
                ClientId = "",
                ClientSecret = "",
                ClientType = "",
                ApplicationType = "",                
                DisplayName = name,
                RedirectUris = { new Uri("https://www.baidu.com/") },
                Permissions = {
                    OpenIddictConstants.Permissions.Endpoints.Authorization,
                    OpenIddictConstants.Permissions.Endpoints.Token,
                    OpenIddictConstants.Permissions.GrantTypes.AuthorizationCode,
                    OpenIddictConstants.Permissions.Scopes.Email,
                    OpenIddictConstants.Permissions.Scopes.Profile,
                    OpenIddictConstants.Permissions.Prefixes.Scope +"api"
                }
            };

            var result = await applicationManager.CreateAsync(info);
            
            return Ok(result);
        }


    }
}
